<?php
/**
 * 网站助手模块
 * @author	yrq
 * @date	2013-04-22
 */

defined('STYLE_URL') or define('STYLE_URL',RES_URL.'/min/f=');	// 定义css样式及js脚本域名前缀

class CscHtml extends CHtml{
	
	/**
	 * 引入前端样式或脚本的方法
	 * @param string $cssurl	// css样式或js脚本地址
	 * @param string $version	// 版本号
	 * @param return string		// 返回css样式或js脚本地址
	 * example					// CscHtml::importCss('a.css,b.css,c.css')
	 */
	public static function importCss($resurl, $version = '',$type=false){
		Global $cscCSS;
		$csses	= explode(',', $resurl);
		$cscCSS = $cscCSS? $cscCSS:array();
		$string	= '';
		if($csses && is_array($csses)){
			$i	= 0;
			foreach($csses as $value){
				$value	= trim($value);				// 清除两边空格
				if(!in_array($value, $cscCSS)){
					if($i==0){
						$string = $value;
					}else{
						$string .= ','.$value;
					}
					array_push($cscCSS, $value);
					$i++;
				}
			}
			$v	= $version? '?v='.$version:'';
			$string = $string? STYLE_URL.$string.$v:'';
		}
		if($type){
			return $string? '<link rel="stylesheet" href="'.$string.'" />':'';
		}
		echo $string? '<link rel="stylesheet" href="'.$string.'" />':'';
	}
	


	/**
	 * 引入前端样式或脚本的方法
	 * @param string $cssurl	// js脚本地址
	 * @param string $version	// 版本号
	 * @param return string		// 返回js脚本地址
	 * example					// CscHtml::importJs('a.css,b.css,c.css')
	 */
	public static function importJs($resurl, $version = '',$type=false){
		Global $cscJs;
		$csses	= explode(',', $resurl);
		$cscJs = $cscJs? $cscJs:array();
		$string	= '';
		if($csses && is_array($csses)){
			$i	= 0;
			foreach($csses as $value){
				$value	= trim($value);				// 清除两边空格
				if(!in_array($value, $cscJs)){
					if($i==0){
						$string = $value;
					}else{
						$string .= ','.$value;
					}
					array_push($cscJs, $value);
					$i++;
				}
			}
			$v	= $version? '?v='.$version:'';
			$string = $string? STYLE_URL.$string.$v:'';
		}
		if($type){
			return $string? '<script src="'.$string.'"></script>':'';
		}
		echo $string? '<script src="'.$string.'"></script>':'';
	}

	/**
	 * 做分页的插件
	 * @param  array('page'=>$this->page, 'ptotal'=>$this->ptotal, 'url'=>$this->url)
	 * */
	public static function Cscpage($array){
		$str = '<span>'.$array['page'].'/<span class="p-total">'.$array['ptotal'].'</span> 页</span>';
		if($array['page'] != 1 && $array['ptotal'] != 0){
			$str .= "<a href='".formatUrl($array['url'], array('page'=>$array['page']-1))."'>上一页</a>";
		}
		if($array['page'] != $array['ptotal'] && $array['ptotal'] != 0){
			$str .= "<a href='".formatUrl($array['url'], array('page'=>$array['page']+1))."'>下一页</a>";
		}
		if($array['ptotal']>1){
			//$str .= '<a href="javascript:" onclick="csc.afPage(this)">跳转</a>';
		}
		//return  $str.CscHtml::importJs('js/m/page.js',null,true);//引入JS
		return $str;
	}
	
	/**
	 * php模拟post|get提交数据			
	 * @author	yrq
	 * @param string  $sendUrl		发送地址,能不带有参数
	 * @param array   $sendData		发送数据
	 * @param string  $sendMethod	发送方式post|get
	 * @return Ambigous <string, mixed>
	 */
	public static function cscPostGet($sendUrl, $sendData, $sendMethod='GET',$https=false){

		if ($sendMethod != 'POST') {
			$sendMethod	= 'GET';
		}
		if( function_exists('curl_init') ) {
			$ch = curl_init();
			
			if ($https) {
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
				curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
			}
		
			if ($sendMethod == 'POST') {
				curl_setopt($ch, CURLOPT_URL, $sendUrl);
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $sendData);
			}else {
				$sendUrl	=  $sendUrl.'?' .($sendData?http_build_query($sendData):"");
				curl_setopt($ch, CURLOPT_URL, $sendUrl);				
			}
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
			try{
				$result = curl_exec($ch);
			}catch (Exception $e){
				echo $e;
			}

			curl_close($ch);
		}else {
			die('this method need surport curl');
			$result	= false;
		}
		return $result;
	}
	/**
	 * 重写父类方法,避免因参数问题导致报错的情况。
	 * @author	yrq
	 * @param unknown $string
	 * @return string
	 */
	public static function encode($string){
		if (is_array($string)||is_object($string)||is_resource($string)) {
			return '';
		}else {
			return parent::encode($string);
		}
	}
		
}